class client{
	construct(protocol,addr,port,client_type,expires){
		this.protocol=protocol;
		this.addr=addr;
		this.port=port;
		this.socket=null;
		this.client_type=client_type;
		this.input=[];
		this.output=[];
		this.client_type=client_type;  //num
		this.client_type_str='';
		this.uuid=createByUuidGeneration;
		this.ws=null;
		this.expires=expires;
		this.info=[];
		this.server_expires=30;
		this.server_connect_status=-1;
		this.client_stage=0;
		this.ob_lock=1;
		
	}
	
	function getConnectStatus(){
		return this.ws.readyState;
	}
	
	function generateUrl(){
		return this.protocol+'://'+this.addr+':'+this.port;
	}
	
	function initInfo(){
		//获取系统生成的uuid
		var that=this;
		var cur=0;
		var t=setInterval(function(){
			if(that.client_stage==0){
				if(cur>that.server_expires){
					if(this.client_stage != 1){
						console.log('分配编号失败,请联系网站管理员或者重试');
					}else{
						that.client_stage=1;
						console.log('分配编号成功');
					}
					clearInterval(t);
					return;
				}
				axios.get('/uuid?type='+that.client_type)
					.then(function(response){
						response=JSON.stringify(response);
						response=eval('('+response+')');
						that.client_type_str=response['type_str'];
						that.info[response['type_str']]=response['uuid'];
						that.client_stage=1;
					}).catch(function(error){
						console.log('当前分配编号失败,正在为你重新分配');
					});
			}else if(that.client_stage==1){
				console.log("分配编号成功");
				clearInterval(t);
			}else{
				console.log("发现未知错误");
				clearInterval(t);
			}
			cur++;
		},500);
	}
	
	function connect(){
		if(this.client_stage != 1){
			console.log('请先分配编号');
			return;
		}
		if(this.ws != null){
			console.log("你已经连接了,请不要重复操作");
			return;
		}
		this.ws=new WebSocket(this.generateUrl());
		var that=this;
		var cur=0;
		var t=setInterval(function(){
			if(that.client_stage==1){
				if(cur>that.server_expires){
					if(that.client_stage != 2){
						console.log('连接失败');
					}else{
						console.log('连接成功');
					}
					clearInterval(t);
					return;
				}
				if(that.ws.readyState == that.ws.CLOSED || that.ws.readyState== that.ws.CLOSING){
					if(cur>that.server_expires){
						console.log("尝试连接中");
						clearInterval(t);
						return;
					}
					that.ws=new WebSocket(that.generateUrl());
				}else{
					that.client_stage=2;
					console.log("连接成功");
					clearInterval(t);
					return;
				}
			}else if(that.client_stage==2){
				console.log("连接成功");
				clearInterval(t);
				return;
			}else{
				console.log("未知错误");
				clearInterval(t);
				return;
			}
			cur++;
		},500);
	}
	
	function login(){
		
		
	}
	
	function createLoginData(){
		//login数据包
		var login_data={
			'type':login,
			'user_type':this.client_type,
			'id':this.info[this.client_type_str],
		};
		login_data[this.client_type_str]=this.info[this.client_type_str];
		return login_data;
	}
	
	function createBtData(){
		//心跳包
		var bt_data={
			'type':'bt',
			'user_type':this.client_type,
			'id':this.info[this.client_type_str],
		};
		bt_data[this.client_type_str]=this.info[this.client_type_str];
		return bt_data;
	}
	
	function createMsData(content){
		//消息包
		var ms_data={
			'type':'ms',
			'sender_type':this.client_type,
			'receiver_type':this.info['countpart']['type'],
			'sender_id':this.info[this.client_type_str],
			'receiver_id':this.info['countpart']['id'],
			'content':content,
		};
		
		return ms_data;
	}
	
	function createLogoutData(){
		//注销包
		var logout_data={
			'type':'logout',
			'user_type':this.client_type,
			'id':this.info[this.client_type_str],
		};
		return logout_data;
	}
	
	function createPongData(){
		var pong_data={
			'type':'ping',
			'countpart_type':this.info['countpart']['type'],
			'countpart_id':this.info['countpart']['id'],
		};
		return pong_data;
	}
	function logout(){
		let logout_data=this.createLogoutData();
		this.ws.send(logout_data);
	}
	
	function receiveMsg(){
		
	}
	
	function sendMsg(content){
		let sm_data=this.createMsData(content);
		this.ws.send(sm_data);
	}
	
	function test(){
		let ping_data=this.createPongData();
		this.ws.send(ping_data);
	}
	
	function sendBtMsg(){
		let bt_data=this.createBtData();
		this.ws.send(bt_data);
	}
	
	function sendLoginMsg(){
		let login_data=this.createLoginData();
		this.ws.send(login_data);
	}
	
	function encodeMsg(msg){
		return JSON.stringify(msg);
	}
	
	function decodeMsg(msg){
		return eval('('+JSON.stringify(msg)+')');
	}
	
	function encryptMsg(msg){
		let key='';
	}
	
	function decryptMsg(msg){
		
	}
}

//一个页面只能一个client防止连接冲突造成异常(单列工厂)
var clientFactory=function(){
	this.client=null;
}
clientFactory.prototype.createClient=function(protocol,addr,port,client_type,expires){
	if(this.client != null){
		return this.client;
	}
	this.client=new client(protocol,addr,port,client_type,expires);
}

export clientFactory;